<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月27日 下午4:37:45 
 * 类说明
 */
class MeetingplaceAction extends BaseAction{
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		return $map;
	}
	
	//配置产品
	
	public function cog(){
		$Model = M($this->getActionName());
		$Shop = M("Shop");
		$shop_list = $Shop->where(array('flag'=>1,'create_userid'=>$this->uid))->select();
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$TypeModel = M("Type");
		$condition['reserve'] = 1;
		$condition['flag'] = 1;
		$types = $TypeModel->where($condition)->field("id,type_name as name")->order("seq asc")->select();
		$this->assign("types",$types);
		$this->assign("vo",$vo);
		$this->assign("shop_list",$shop_list);
		$this->display();
	}

	//已添加产品列表
	public function getMeetingplaceProductData(){
		$Model = M("MeetingplaceProduct");
		$where = $this->loadSeachCondition();
		$where['meetingplace_id'] = $_REQUEST['id'];
		$where['shop_id'] = $_REQUEST['shop_id'];
		$total = $Model->where($where)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$map = $this->loadProductSeachCondition();
		$map['b.meetingplace_id'] = $_REQUEST['id'];
		if(!empty($page->sort)){
			if($page->sort=="id desc"){
				$sort = 'b.'.$page->sort;
			}else if($page->sort=="name desc"){
				$sort = 'a.'.$page->sort;
			}
		}else{
			$sort = 'b.'.$page->sort;
		}
		if(isset($_REQUEST['shop_id'])){
			$map['b.shop_id'] = $_REQUEST['shop_id'];
		}
		$data = $Model->alias('b')->join('left join __PRODUCT__ as a on a.id = b.product_id')->field('b.id,a.name as name')->where($map)->limit($page->firstRow,$page->listRows)->order($sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	//产品列表
	public function getProductAllData(){
		$Model = M("Product");
		$where = $this->loadSeachCondition();
		if(isset($_REQUEST['shop_id'])){
			$where['shop_id'] = $_REQUEST['shop_id'];
		}
		$where['flag'] = 1;
		$total = $Model->where($where)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		if(!empty($page->sort)){
			$sort = 'a.'.$page->sort;
		}else{
			$sort = $page->sort;
		}
		$map = $this->loadProductSeachCondition();
		$map['a.flag'] = 1;
		if(isset($_REQUEST['shop_id'])){
			$map['c.shop_id'] = $_REQUEST['shop_id'];
		}
		$map['c.flag'] = 1;
		$data = M("ShopProduct")->alias('c')->join('left join __PRODUCT__ as a on c.product_id=a.id left join __TYPE__ as b on b.id = a.type_id')->field('a.id,a.name,a.spec,a.desc,a.price,a.is_on,a.seq,a.create_time,b.type_name')->where($map)->limit($page->firstRow,$page->listRows)->order($sort)->select();

		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadProductSeachCondition(){
		$map = array();
		if(!empty($_GET['name'])){
			$map['a.name'] = array("like","%".$_GET['name']."%");
		}
		if(!empty($_GET['type_id'])){
			$map['a.type_id'] = $_GET['type_id'];
		}
		return $map;
	}
	
	/**
	 * 添加至列表
	 */
	public function addToList(){
		$MeetingplaceProductModel = M('MeetingplaceProduct');
		$data['product_id'] = $_REQUEST["product_id"];
		$data['meetingplace_id'] = $_REQUEST["meetingplace_id"];
		if(isset($_REQUEST['shop_id'])){
			$data['shop_id'] = $_REQUEST['shop_id'];
		}
		$count = $MeetingplaceProductModel->where($data)->count();

		if($count == 0){
			$MeetingplaceProductModel->add($data);
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),0);
		}else{
			$this->ajaxReturn(0,"重复添加！",1);
		}
	}
	/**
	 * @authority 修改店铺商品排序
	 */
	public function editProductSeq() {
		$Shop = M("Shop");
		$shop_list = $Shop->where(array('flag'=>1,'create_userid'=>$this->uid))->select();
		$this->assign("shop_list",$shop_list);
		$this->assign("meetingplace_id",$_REQUEST['id']);
		$this->display();
	}
	public function getNodes(){
		$Model = D("MeetingplaceProduct");
		$map['a.meetingplace_id'] = $_REQUEST['id'];
		$map['a.shop_id'] = $_REQUEST['shop_id'];
		if($_REQUEST['shop_id']==0){
			$this->ajaxReturn(array(),'',1);
		}
		$data =  $Model->alias('a')->join('left join __PRODUCT__ as b on b.id = a.product_id')->where($map)->order('a.seq asc')->field('a.id,b.name as name')->select();
		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "店铺商品";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}
		$this->ajaxReturn($newData,'',1);
	}
	/**
	 * @authority 更新店铺商品排序
	 */
	public function updateProductSeq(){
		$Model = D("MeetingplaceProduct");
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			//$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}
}